{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31省市居民家庭消费调查（K-Means）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "city=pd.read_csv('./data/31省市居民家庭消费调查/city.txt',encoding='gbk',header=None,engine='python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['北京', '天津', '河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '上海', '江苏', '浙江',\n",
       "       '安徽', '福建', '江西', '山东', '河南', '湖南', '湖北', '广东', '广西', '海南', '重庆',\n",
       "       '四川', '贵州', '云南', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆'], dtype=object)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cityName=city.iloc[:,0].values\n",
    "cityName"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2959.19</td>\n",
       "      <td>730.79</td>\n",
       "      <td>749.41</td>\n",
       "      <td>513.34</td>\n",
       "      <td>467.87</td>\n",
       "      <td>1141.82</td>\n",
       "      <td>478.42</td>\n",
       "      <td>457.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2459.77</td>\n",
       "      <td>495.47</td>\n",
       "      <td>697.33</td>\n",
       "      <td>302.87</td>\n",
       "      <td>284.19</td>\n",
       "      <td>735.97</td>\n",
       "      <td>570.84</td>\n",
       "      <td>305.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1495.63</td>\n",
       "      <td>515.90</td>\n",
       "      <td>362.37</td>\n",
       "      <td>285.32</td>\n",
       "      <td>272.95</td>\n",
       "      <td>540.58</td>\n",
       "      <td>364.91</td>\n",
       "      <td>188.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1406.33</td>\n",
       "      <td>477.77</td>\n",
       "      <td>290.15</td>\n",
       "      <td>208.57</td>\n",
       "      <td>201.50</td>\n",
       "      <td>414.72</td>\n",
       "      <td>281.84</td>\n",
       "      <td>212.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1303.97</td>\n",
       "      <td>524.29</td>\n",
       "      <td>254.83</td>\n",
       "      <td>192.17</td>\n",
       "      <td>249.81</td>\n",
       "      <td>463.09</td>\n",
       "      <td>287.87</td>\n",
       "      <td>192.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1730.84</td>\n",
       "      <td>553.90</td>\n",
       "      <td>246.91</td>\n",
       "      <td>279.81</td>\n",
       "      <td>239.18</td>\n",
       "      <td>445.20</td>\n",
       "      <td>330.24</td>\n",
       "      <td>163.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1561.86</td>\n",
       "      <td>492.42</td>\n",
       "      <td>200.49</td>\n",
       "      <td>218.36</td>\n",
       "      <td>220.69</td>\n",
       "      <td>459.62</td>\n",
       "      <td>360.48</td>\n",
       "      <td>147.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1410.11</td>\n",
       "      <td>510.71</td>\n",
       "      <td>211.88</td>\n",
       "      <td>277.11</td>\n",
       "      <td>224.65</td>\n",
       "      <td>376.82</td>\n",
       "      <td>317.61</td>\n",
       "      <td>152.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3712.31</td>\n",
       "      <td>550.74</td>\n",
       "      <td>893.37</td>\n",
       "      <td>346.93</td>\n",
       "      <td>527.00</td>\n",
       "      <td>1034.98</td>\n",
       "      <td>720.33</td>\n",
       "      <td>462.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2207.58</td>\n",
       "      <td>449.37</td>\n",
       "      <td>572.40</td>\n",
       "      <td>211.92</td>\n",
       "      <td>302.09</td>\n",
       "      <td>585.23</td>\n",
       "      <td>429.77</td>\n",
       "      <td>252.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2629.16</td>\n",
       "      <td>557.32</td>\n",
       "      <td>689.73</td>\n",
       "      <td>435.69</td>\n",
       "      <td>514.66</td>\n",
       "      <td>795.87</td>\n",
       "      <td>575.76</td>\n",
       "      <td>323.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1844.78</td>\n",
       "      <td>430.29</td>\n",
       "      <td>271.28</td>\n",
       "      <td>126.33</td>\n",
       "      <td>250.56</td>\n",
       "      <td>513.18</td>\n",
       "      <td>314.00</td>\n",
       "      <td>151.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2709.46</td>\n",
       "      <td>428.11</td>\n",
       "      <td>334.12</td>\n",
       "      <td>160.77</td>\n",
       "      <td>405.14</td>\n",
       "      <td>461.67</td>\n",
       "      <td>535.13</td>\n",
       "      <td>232.29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1563.78</td>\n",
       "      <td>303.65</td>\n",
       "      <td>233.81</td>\n",
       "      <td>107.90</td>\n",
       "      <td>209.70</td>\n",
       "      <td>393.99</td>\n",
       "      <td>509.39</td>\n",
       "      <td>160.12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1675.75</td>\n",
       "      <td>613.32</td>\n",
       "      <td>550.71</td>\n",
       "      <td>219.79</td>\n",
       "      <td>272.59</td>\n",
       "      <td>599.43</td>\n",
       "      <td>371.62</td>\n",
       "      <td>211.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1427.65</td>\n",
       "      <td>431.79</td>\n",
       "      <td>288.55</td>\n",
       "      <td>208.14</td>\n",
       "      <td>217.00</td>\n",
       "      <td>337.76</td>\n",
       "      <td>421.31</td>\n",
       "      <td>165.32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1942.23</td>\n",
       "      <td>512.27</td>\n",
       "      <td>401.39</td>\n",
       "      <td>206.06</td>\n",
       "      <td>321.29</td>\n",
       "      <td>697.22</td>\n",
       "      <td>492.60</td>\n",
       "      <td>226.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1783.43</td>\n",
       "      <td>511.88</td>\n",
       "      <td>282.84</td>\n",
       "      <td>201.01</td>\n",
       "      <td>237.60</td>\n",
       "      <td>617.74</td>\n",
       "      <td>523.52</td>\n",
       "      <td>182.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>3055.17</td>\n",
       "      <td>353.23</td>\n",
       "      <td>564.56</td>\n",
       "      <td>356.27</td>\n",
       "      <td>811.88</td>\n",
       "      <td>873.06</td>\n",
       "      <td>1082.82</td>\n",
       "      <td>420.81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2033.87</td>\n",
       "      <td>300.82</td>\n",
       "      <td>338.65</td>\n",
       "      <td>157.78</td>\n",
       "      <td>329.06</td>\n",
       "      <td>621.74</td>\n",
       "      <td>587.02</td>\n",
       "      <td>218.27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2057.86</td>\n",
       "      <td>186.44</td>\n",
       "      <td>202.72</td>\n",
       "      <td>171.79</td>\n",
       "      <td>329.65</td>\n",
       "      <td>477.17</td>\n",
       "      <td>312.93</td>\n",
       "      <td>279.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2303.29</td>\n",
       "      <td>589.99</td>\n",
       "      <td>516.21</td>\n",
       "      <td>236.55</td>\n",
       "      <td>403.92</td>\n",
       "      <td>730.05</td>\n",
       "      <td>438.41</td>\n",
       "      <td>225.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1974.28</td>\n",
       "      <td>507.76</td>\n",
       "      <td>344.79</td>\n",
       "      <td>203.21</td>\n",
       "      <td>240.24</td>\n",
       "      <td>575.10</td>\n",
       "      <td>430.36</td>\n",
       "      <td>223.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1673.82</td>\n",
       "      <td>437.75</td>\n",
       "      <td>461.61</td>\n",
       "      <td>153.32</td>\n",
       "      <td>254.66</td>\n",
       "      <td>445.59</td>\n",
       "      <td>346.11</td>\n",
       "      <td>191.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2194.25</td>\n",
       "      <td>537.01</td>\n",
       "      <td>369.07</td>\n",
       "      <td>249.54</td>\n",
       "      <td>290.84</td>\n",
       "      <td>561.91</td>\n",
       "      <td>407.70</td>\n",
       "      <td>330.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2646.61</td>\n",
       "      <td>839.70</td>\n",
       "      <td>204.44</td>\n",
       "      <td>209.11</td>\n",
       "      <td>379.30</td>\n",
       "      <td>371.04</td>\n",
       "      <td>269.59</td>\n",
       "      <td>389.33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1472.95</td>\n",
       "      <td>390.89</td>\n",
       "      <td>447.95</td>\n",
       "      <td>259.51</td>\n",
       "      <td>230.61</td>\n",
       "      <td>490.90</td>\n",
       "      <td>469.10</td>\n",
       "      <td>191.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1525.57</td>\n",
       "      <td>472.98</td>\n",
       "      <td>328.90</td>\n",
       "      <td>219.86</td>\n",
       "      <td>206.65</td>\n",
       "      <td>449.69</td>\n",
       "      <td>249.66</td>\n",
       "      <td>228.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1654.69</td>\n",
       "      <td>437.77</td>\n",
       "      <td>258.78</td>\n",
       "      <td>303.00</td>\n",
       "      <td>244.93</td>\n",
       "      <td>479.53</td>\n",
       "      <td>288.56</td>\n",
       "      <td>236.51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1375.46</td>\n",
       "      <td>480.89</td>\n",
       "      <td>273.84</td>\n",
       "      <td>317.32</td>\n",
       "      <td>251.08</td>\n",
       "      <td>424.75</td>\n",
       "      <td>228.73</td>\n",
       "      <td>195.93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>1608.82</td>\n",
       "      <td>536.05</td>\n",
       "      <td>432.46</td>\n",
       "      <td>235.82</td>\n",
       "      <td>250.28</td>\n",
       "      <td>541.30</td>\n",
       "      <td>344.85</td>\n",
       "      <td>214.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          1       2       3       4       5        6        7       8\n",
       "0   2959.19  730.79  749.41  513.34  467.87  1141.82   478.42  457.64\n",
       "1   2459.77  495.47  697.33  302.87  284.19   735.97   570.84  305.08\n",
       "2   1495.63  515.90  362.37  285.32  272.95   540.58   364.91  188.63\n",
       "3   1406.33  477.77  290.15  208.57  201.50   414.72   281.84  212.10\n",
       "4   1303.97  524.29  254.83  192.17  249.81   463.09   287.87  192.96\n",
       "5   1730.84  553.90  246.91  279.81  239.18   445.20   330.24  163.86\n",
       "6   1561.86  492.42  200.49  218.36  220.69   459.62   360.48  147.76\n",
       "7   1410.11  510.71  211.88  277.11  224.65   376.82   317.61  152.85\n",
       "8   3712.31  550.74  893.37  346.93  527.00  1034.98   720.33  462.03\n",
       "9   2207.58  449.37  572.40  211.92  302.09   585.23   429.77  252.54\n",
       "10  2629.16  557.32  689.73  435.69  514.66   795.87   575.76  323.36\n",
       "11  1844.78  430.29  271.28  126.33  250.56   513.18   314.00  151.39\n",
       "12  2709.46  428.11  334.12  160.77  405.14   461.67   535.13  232.29\n",
       "13  1563.78  303.65  233.81  107.90  209.70   393.99   509.39  160.12\n",
       "14  1675.75  613.32  550.71  219.79  272.59   599.43   371.62  211.84\n",
       "15  1427.65  431.79  288.55  208.14  217.00   337.76   421.31  165.32\n",
       "16  1942.23  512.27  401.39  206.06  321.29   697.22   492.60  226.45\n",
       "17  1783.43  511.88  282.84  201.01  237.60   617.74   523.52  182.52\n",
       "18  3055.17  353.23  564.56  356.27  811.88   873.06  1082.82  420.81\n",
       "19  2033.87  300.82  338.65  157.78  329.06   621.74   587.02  218.27\n",
       "20  2057.86  186.44  202.72  171.79  329.65   477.17   312.93  279.19\n",
       "21  2303.29  589.99  516.21  236.55  403.92   730.05   438.41  225.80\n",
       "22  1974.28  507.76  344.79  203.21  240.24   575.10   430.36  223.46\n",
       "23  1673.82  437.75  461.61  153.32  254.66   445.59   346.11  191.48\n",
       "24  2194.25  537.01  369.07  249.54  290.84   561.91   407.70  330.95\n",
       "25  2646.61  839.70  204.44  209.11  379.30   371.04   269.59  389.33\n",
       "26  1472.95  390.89  447.95  259.51  230.61   490.90   469.10  191.34\n",
       "27  1525.57  472.98  328.90  219.86  206.65   449.69   249.66  228.19\n",
       "28  1654.69  437.77  258.78  303.00  244.93   479.53   288.56  236.51\n",
       "29  1375.46  480.89  273.84  317.32  251.08   424.75   228.73  195.93\n",
       "30  1608.82  536.05  432.46  235.82  250.28   541.30   344.85  214.40"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=city.iloc[:,1:]\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "km=KMeans(n_clusters=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1,\n",
       "       0, 0, 0, 1, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label = km.fit_predict(data)\n",
    "label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1692.41347826,  461.56173913,  331.58173913,  217.98434783,\n",
       "         254.24391304,  500.53304348,  376.96434783,  205.13304348],\n",
       "       [2809.37      ,  568.16875   ,  581.14625   ,  320.19125   ,\n",
       "         474.245     ,  768.0575    ,  583.9125    ,  352.0425    ]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "km.cluster_centers_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4040.41565217, 6457.13375   ])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expenses = np.sum(km.cluster_centers_,axis=1)\n",
    "expenses"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "CityCluster = [[],[]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['河北',\n",
       "  '山西',\n",
       "  '内蒙古',\n",
       "  '辽宁',\n",
       "  '吉林',\n",
       "  '黑龙江',\n",
       "  '江苏',\n",
       "  '安徽',\n",
       "  '江西',\n",
       "  '山东',\n",
       "  '河南',\n",
       "  '湖南',\n",
       "  '湖北',\n",
       "  '广西',\n",
       "  '海南',\n",
       "  '四川',\n",
       "  '贵州',\n",
       "  '云南',\n",
       "  '陕西',\n",
       "  '甘肃',\n",
       "  '青海',\n",
       "  '宁夏',\n",
       "  '新疆'],\n",
       " ['北京', '天津', '上海', '浙江', '福建', '广东', '重庆', '西藏']]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(cityName)):\n",
    "    CityCluster[label[i]].append(cityName[i])\n",
    "CityCluster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Expenses:4040.42\n",
      "['河北', '山西', '内蒙古', '辽宁', '吉林', '黑龙江', '江苏', '安徽', '江西', '山东', '河南', '湖南', '湖北', '广西', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '宁夏', '新疆']\n",
      "Expenses:6457.13\n",
      "['北京', '天津', '上海', '浙江', '福建', '广东', '重庆', '西藏']\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(CityCluster)):\n",
    "    print('Expenses:%.2f' % expenses[i])\n",
    "    print(CityCluster[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 学生开始上网时间分布（DBSCAN）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2c929293466b97a6014754607e457d68</td>\n",
       "      <td>U201215025</td>\n",
       "      <td>A417314EEA7B</td>\n",
       "      <td>10.12.49.26</td>\n",
       "      <td>2014-07-20 22:44:18.540000000</td>\n",
       "      <td>2014-07-20 23:10:16.540000000</td>\n",
       "      <td>1558</td>\n",
       "      <td>15</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>100元每半年</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2c929293466b97a60147546099a57d81</td>\n",
       "      <td>U201116197</td>\n",
       "      <td>F0DEF1C78366</td>\n",
       "      <td>222.20.71.38</td>\n",
       "      <td>2014-07-20 12:14:21.380000000</td>\n",
       "      <td>2014-07-20 23:25:22.380000000</td>\n",
       "      <td>40261</td>\n",
       "      <td>1</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>20元每月</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2c929293466b97a60147546099fa7d86</td>\n",
       "      <td>M201373803</td>\n",
       "      <td>88539523E88D</td>\n",
       "      <td>10.12.59.230</td>\n",
       "      <td>2014-07-20 22:56:41.593000000</td>\n",
       "      <td>2014-07-20 23:25:22.593000000</td>\n",
       "      <td>1721</td>\n",
       "      <td>15</td>\n",
       "      <td>研究生动态IP模版</td>\n",
       "      <td>计天</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2c929293466b97a6014754609a137d88</td>\n",
       "      <td>M201370611</td>\n",
       "      <td>F0DEF167324F</td>\n",
       "      <td>218.197.241.94</td>\n",
       "      <td>2014-07-20 23:19:30.930000000</td>\n",
       "      <td>2014-07-20 23:25:21.930000000</td>\n",
       "      <td>351</td>\n",
       "      <td>1</td>\n",
       "      <td>研究生动态IP模版</td>\n",
       "      <td>20元包月</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2c929293466b97a601475460ab577d99</td>\n",
       "      <td>U201112081</td>\n",
       "      <td>B888E3813D3C</td>\n",
       "      <td>218.197.229.5</td>\n",
       "      <td>2014-07-20 16:51:56.657000000</td>\n",
       "      <td>2014-07-20 23:24:40.657000000</td>\n",
       "      <td>23564</td>\n",
       "      <td>1</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>1元每天</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>284</th>\n",
       "      <td>2c929293466b97a601475645b36d4b85</td>\n",
       "      <td>M201372774</td>\n",
       "      <td>68DFDD6703AB</td>\n",
       "      <td>10.10.194.32</td>\n",
       "      <td>2014-07-21 07:55:08.247000000</td>\n",
       "      <td>2014-07-21 08:00:14.247000000</td>\n",
       "      <td>306</td>\n",
       "      <td>15</td>\n",
       "      <td>研究生动态IP模版</td>\n",
       "      <td>20元包月</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>285</th>\n",
       "      <td>2c929293466b97a60147564531de4abf</td>\n",
       "      <td>D201377117</td>\n",
       "      <td>041BBA75CF7A</td>\n",
       "      <td>10.10.24.154</td>\n",
       "      <td>2014-07-21 07:47:43.700000000</td>\n",
       "      <td>2014-07-21 08:14:41.700000000</td>\n",
       "      <td>1618</td>\n",
       "      <td>15</td>\n",
       "      <td>研究生动态IP模版</td>\n",
       "      <td>计天</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>286</th>\n",
       "      <td>2c929293466b97a6014756454e814ae5</td>\n",
       "      <td>U201013575</td>\n",
       "      <td>68DFDD27E77D</td>\n",
       "      <td>10.12.63.48</td>\n",
       "      <td>2014-07-21 07:45:42.407000000</td>\n",
       "      <td>2014-07-21 08:14:48.407000000</td>\n",
       "      <td>1746</td>\n",
       "      <td>15</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>5元每周</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>287</th>\n",
       "      <td>2c929293466b97a60147564551ba4aea</td>\n",
       "      <td>422123640321732</td>\n",
       "      <td>3CDFBD175878</td>\n",
       "      <td>10.12.85.159</td>\n",
       "      <td>2014-07-21 08:05:49.327000000</td>\n",
       "      <td>2014-07-21 08:14:49.273000000</td>\n",
       "      <td>540</td>\n",
       "      <td>15</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>20元每月</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>288</th>\n",
       "      <td>2c929293466b97a601475645551d4af3</td>\n",
       "      <td>U201014641</td>\n",
       "      <td>002427FE3712</td>\n",
       "      <td>0.0.0.0</td>\n",
       "      <td>2014-07-21 08:14:29.287000000</td>\n",
       "      <td>2014-07-21 08:14:49.287000000</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>本科生动态IP模版</td>\n",
       "      <td>100元每半年</td>\n",
       "      <td>internet</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>289 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   0                1             2   \\\n",
       "0    2c929293466b97a6014754607e457d68       U201215025  A417314EEA7B   \n",
       "1    2c929293466b97a60147546099a57d81       U201116197  F0DEF1C78366   \n",
       "2    2c929293466b97a60147546099fa7d86       M201373803  88539523E88D   \n",
       "3    2c929293466b97a6014754609a137d88       M201370611  F0DEF167324F   \n",
       "4    2c929293466b97a601475460ab577d99       U201112081  B888E3813D3C   \n",
       "..                                ...              ...           ...   \n",
       "284  2c929293466b97a601475645b36d4b85       M201372774  68DFDD6703AB   \n",
       "285  2c929293466b97a60147564531de4abf       D201377117  041BBA75CF7A   \n",
       "286  2c929293466b97a6014756454e814ae5       U201013575  68DFDD27E77D   \n",
       "287  2c929293466b97a60147564551ba4aea  422123640321732  3CDFBD175878   \n",
       "288  2c929293466b97a601475645551d4af3       U201014641  002427FE3712   \n",
       "\n",
       "                 3                              4   \\\n",
       "0       10.12.49.26  2014-07-20 22:44:18.540000000   \n",
       "1      222.20.71.38  2014-07-20 12:14:21.380000000   \n",
       "2      10.12.59.230  2014-07-20 22:56:41.593000000   \n",
       "3    218.197.241.94  2014-07-20 23:19:30.930000000   \n",
       "4     218.197.229.5  2014-07-20 16:51:56.657000000   \n",
       "..              ...                            ...   \n",
       "284    10.10.194.32  2014-07-21 07:55:08.247000000   \n",
       "285    10.10.24.154  2014-07-21 07:47:43.700000000   \n",
       "286     10.12.63.48  2014-07-21 07:45:42.407000000   \n",
       "287    10.12.85.159  2014-07-21 08:05:49.327000000   \n",
       "288         0.0.0.0  2014-07-21 08:14:29.287000000   \n",
       "\n",
       "                                5      6   7          8        9         10  \n",
       "0    2014-07-20 23:10:16.540000000   1558  15  本科生动态IP模版  100元每半年  internet  \n",
       "1    2014-07-20 23:25:22.380000000  40261   1  本科生动态IP模版    20元每月  internet  \n",
       "2    2014-07-20 23:25:22.593000000   1721  15  研究生动态IP模版       计天  internet  \n",
       "3    2014-07-20 23:25:21.930000000    351   1  研究生动态IP模版    20元包月  internet  \n",
       "4    2014-07-20 23:24:40.657000000  23564   1  本科生动态IP模版     1元每天  internet  \n",
       "..                             ...    ...  ..        ...      ...       ...  \n",
       "284  2014-07-21 08:00:14.247000000    306  15  研究生动态IP模版    20元包月  internet  \n",
       "285  2014-07-21 08:14:41.700000000   1618  15  研究生动态IP模版       计天  internet  \n",
       "286  2014-07-21 08:14:48.407000000   1746  15  本科生动态IP模版     5元每周  internet  \n",
       "287  2014-07-21 08:14:49.273000000    540  15  本科生动态IP模版    20元每月  internet  \n",
       "288  2014-07-21 08:14:49.287000000     20   1  本科生动态IP模版  100元每半年  internet  \n",
       "\n",
       "[289 rows x 11 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.read_csv('./data/学生月上网时间分布/TestData.txt',header=None)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      2014-07-20 22:44:18.540000000\n",
       "1      2014-07-20 12:14:21.380000000\n",
       "2      2014-07-20 22:56:41.593000000\n",
       "3      2014-07-20 23:19:30.930000000\n",
       "4      2014-07-20 16:51:56.657000000\n",
       "                   ...              \n",
       "284    2014-07-21 07:55:08.247000000\n",
       "285    2014-07-21 07:47:43.700000000\n",
       "286    2014-07-21 07:45:42.407000000\n",
       "287    2014-07-21 08:05:49.327000000\n",
       "288    2014-07-21 08:14:29.287000000\n",
       "Name: 4, Length: 289, dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[:,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "starttime=pd.to_datetime(data.iloc[:,4]).dt.hour\n",
    "X=starttime.values.reshape(-1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "db=DBSCAN(eps=0.01,min_samples=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels=db.fit_predict(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Noise ratio: 0.22145328719723184\n"
     ]
    }
   ],
   "source": [
    "print('Noise ratio:',len(labels[labels==-1]) / len(labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_clusters_=len(set(labels))-(1 if -1 in labels else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7104124919280866"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.silhouette_score(X,labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cluster  0 :\n",
      "[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22]\n",
      "Cluster  1 :\n",
      "[23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23]\n",
      "Cluster  2 :\n",
      "[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]\n",
      "Cluster  3 :\n",
      "[21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21]\n",
      "Cluster  4 :\n",
      "[8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]\n",
      "Cluster  5 :\n",
      "[7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]\n"
     ]
    }
   ],
   "source": [
    "for i in range(n_clusters_):\n",
    "    print('Cluster ',i,':')\n",
    "    print(list(X[labels==i].flatten()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 16, 19, 18, 16, 18, 17, 16, 15, 15, 19, 14, 14, 10, 19, 18, 19,\n",
       "       15, 12, 18,  9, 15, 10, 17, 11, 16, 16, 15, 10, 18, 12,  5, 16, 19,\n",
       "       10, 10, 11, 11,  9, 11, 10, 14,  9, 17, 19, 13, 12, 10, 18, 18, 10,\n",
       "       18, 19, 13, 12, 13, 14, 16,  5, 13,  6,  0,  2,  6], dtype=int64)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[labels==-1].flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 1.,  0.,  1.,  0.,  0.,  2.,  2., 24., 28.,  3.,  8.,  4.,  5.,\n",
       "         4.,  4.,  5.,  7.,  3.,  8.,  7., 28., 25., 55., 65.]),\n",
       " array([ 0.        ,  0.95833333,  1.91666667,  2.875     ,  3.83333333,\n",
       "         4.79166667,  5.75      ,  6.70833333,  7.66666667,  8.625     ,\n",
       "         9.58333333, 10.54166667, 11.5       , 12.45833333, 13.41666667,\n",
       "        14.375     , 15.33333333, 16.29166667, 17.25      , 18.20833333,\n",
       "        19.16666667, 20.125     , 21.08333333, 22.04166667, 23.        ]),\n",
       " <a list of 24 Patch objects>)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image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6+qvgP8eMbwNmCyW58Etg+1qGVwkeswcqrqRFV9t1t/EzgMXMUI3hMtBPhVwA/P2z7WjY2iAr6d5FD3FQWjbGNVnYDev9DAhmWuZzndl+R7XYtl1bcNzpdkHLgJeIYRvCdaCPC+XtkfEbdW1YfotZPuTfLh5S5Iy+6LwC8AW4ETwF8ubznDk+R9wNeAT1fVT5a7nuXQQoD7yn6nqo53y1PAE/TaS6PqZJJNAN3y1DLXsyyq6mRVnamqd4C/YUTuiSTr6IX3I1X19W545O6JFgLcV/aBJJcmef/ZdeBjwIvvfdSqth/Y0a3vAPYtYy3L5mxgdX6TEbgnkgR4EDhcVZ8/76ORuyeaeBOzezTqC5x7Zf8vlrmkoUvyQXqzbuh9BcKXR+U6JPkK8BF6Xxd6EvgT4O+Ax4BrgNeAe6pqVf+C7yLX4SP02icFHAV+72wfeLVK8ivAPwIvAO90w5+l1wcfrXuihQCXJF2ohRaKJGkWBrgkNcoAl6RGGeCS1CgDXJIaZYBLUqMMcElq1P8BpeSyrHeRQqQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(X,24,edgecolor='white')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于聚类的图像分割（K-Means）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import PIL.Image as image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadData(filePath):\n",
    "    f=open(filePath,'rb')\n",
    "    data=[]\n",
    "    img=image.open(f)\n",
    "    m,n=img.size\n",
    "    for i in range(m):\n",
    "        for j in range(n):\n",
    "            x,y,z=img.getpixel((i,j))\n",
    "            data.append([x/256.0,y/256.0,z/256.0])\n",
    "    f.close()\n",
    "    return np.mat(data),m,n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgData,row,col=loadData('./data/基于聚类的图像分割/bull.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        ...,\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "640"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "360"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "km=KMeans(n_clusters=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels=km.fit_predict(imgData)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels=labels.reshape(row,col)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "pic_new=image.new('L',(row,col))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(row):\n",
    "    for j in range(col):\n",
    "        pic_new.putpixel((i,j),int(256/(labels[i,j]+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "pic_new.save('./data/基于聚类的图像分割/result_bull.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
